#ifndef DATAMANAGER_H
#define DATAMANAGER_H

#include "sortfiltertablemodel.h"
#include "database.h"
#include "tableschema.h"

class TableViewBase;
class DataTableManagerBase : public QObject
{
    Q_OBJECT

public:
    DataTableManagerBase( QObject * parent );
    virtual ~DataTableManagerBase();

    SortFilterTableModel & sortModel();
    const SortFilterTableModel & sortModel() const;

    QStandardItemModel & tableModelBase();
    const QStandardItemModel & tableModelBase() const;

    bool select( const QString & filterStr );
    bool select( const QString & filterStr, TableViewBase & tableViewBase );
    void selectRow( int standardModelRow );
    void removeRow( int standardModelRow );
    bool getStandardModelRowAndStandardItemFromProxyIndex( const QModelIndex & proxyModelIndex, int & standardModelRow, const QStandardItem *& item ) const;
    bool getStandardModelRowAndStandardItemFromProxyIndex( const QItemSelectionModel * selectionModel, int & standardModelRow, const QStandardItem *& item ) const;

signals:
    void selectRowSignal( int row );

protected:
    virtual bool selectToTableModel( const QString & filterStr ) = 0;

protected:
    QStandardItemModel m_standardModel;
    SortFilterTableModel m_sortFilterProxyModel;

private:
    bool _select( const QString & filterStr, TableViewBase & tableViewBase, QVariant & selectedData );
    bool _getStandardModelRowAndStandardItemFromProxyIndex( const QModelIndex & proxyModelIndex, int & standardModelRow, const QStandardItem *& item ) const;
};

typedef QVector<const QStandardItem *> StandardItemVector;
typedef QVector<int> StandardModelRowVector;

class ManagerBase
{
public:
    struct ActionStruct
    {
        int actionId;
        QString actionName;
        QString image;
        ActionStruct()
        {
            assert( 0 );
        }
        ActionStruct( int _actionId, const QString & _actionName, const QString & _image = QString() )
        :   actionId( _actionId ),
            actionName( _actionName ),
            image( _image )
        {
        }
    };

    struct ActionBase
    {
        enum Enum
        {
            New,
            Update,
            Delete,
        };
    };

    typedef QVector<ActionStruct> ActionStructVector;
public:

    ManagerBase( QWidget * parent );
    virtual ~ManagerBase();
    virtual DataTableManagerBase & dataTableManager() = 0;

    virtual ActionStructVector createActionList( const QStandardItem * item = NULL ) const;
    virtual ActionStructVector createActionList( const StandardItemVector & standardItemVector ) const;
    virtual void executeAction( int actionId, int standardModelRow = -1, const QStandardItem * item = NULL ) = 0;
    virtual void executeAction( int actionId, const StandardModelRowVector & standardModelRowVector, const StandardItemVector & standardItemVector );
    virtual void executeDoubleClickAction( int standardModelRow, const QStandardItem * item = NULL ) = 0;
    
protected:
    QWidget & m_parent;
};

#endif
